package com.chengcang.common.utils;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 税费计算工具类
 */
public class TaxCalculationUtils {

    public static Map<String,String> tax(String price, String amount){
        BigDecimal priceDecimal = new BigDecimal(price);
        BigDecimal amountDecimal = new BigDecimal(amount);

        //计算税金额
        //卖家收款金额 单价乘以数量
        BigDecimal sellerReceivePrice = priceDecimal.multiply(amountDecimal).setScale(2, RoundingMode.HALF_UP);
        //税率
        BigDecimal taxRate = new BigDecimal("0.001");
        //服务费率
        BigDecimal serviceRate = new BigDecimal("0.001");
        // 1
        BigDecimal unit1 = new BigDecimal(1);

        //不含增值税金额
        BigDecimal noVatPrice = sellerReceivePrice.multiply((serviceRate.add(unit1))).setScale(2,RoundingMode.UP);
        //增值税
        BigDecimal vat = sellerReceivePrice.multiply(taxRate.divide(
                unit1.subtract(taxRate), RoundingMode.UP).setScale(2,RoundingMode.UP)).setScale(2,RoundingMode.UP);
        //合同总价
        BigDecimal contractTotalPrice = sellerReceivePrice
                .multiply((unit1.divide(unit1.subtract(taxRate), RoundingMode.UP).setScale(2,RoundingMode.UP))
                        .add(serviceRate)).setScale(2,RoundingMode.UP);
        //平台代收税费
        BigDecimal platformTakeTaxPrice = sellerReceivePrice.multiply(
                (taxRate.divide(unit1.subtract(taxRate), RoundingMode.UP).setScale(2,RoundingMode.UP))
                        .add(serviceRate)).setScale(2,RoundingMode.UP);
        HashMap<String, String> map = new HashMap<>();
        DecimalFormat df = new DecimalFormat("#,##0.00");
        map.put("sellerReceivePrice",df.format(sellerReceivePrice));
        map.put("vat",df.format(vat));
        map.put("noVatPrice",df.format(noVatPrice));
        map.put("contractTotalPrice",df.format(contractTotalPrice));
        map.put("platformTakeTaxPrice",df.format(platformTakeTaxPrice));
        return map;
    }

    public static void main(String[] args) {
        Map<String, String> tax = tax("100.00", "100");
        System.out.println(tax);
    }
}
